﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Lost_and_Found.Models.DB;
using Lost_and_Found.Models.ViewModel;

namespace Lost_and_Found.Models.ObjectManager
{
    public class TopicManager
    {
        LAFEntities db = new LAFEntities();
        public TopicPage getTopicPage(string type,int IdPage, int sizePage)
        {

            var listtp = db.Topics.Where(a => a.Type == type).ToList();
            listtp.Sort((x1,x2)=>x2.DatePost.CompareTo(x1.DatePost));

            var filter = (from f in listtp select f).Skip((IdPage - 1) * sizePage).Take(sizePage).ToList();
            
            List<TopicModel> listmodel = new List<TopicModel>();
            foreach (var t in filter) 
            {
                listmodel.Add(new TopicModel(t));
            }
            TopicPage tpPage = new TopicPage() { Topics = listmodel, PageId = IdPage, PageSize = sizePage, TopicCount = listtp.Count };
            return (tpPage);
        }

        public TopicModel getTopicModel(int TopicID)
        {
            Topic tp = db.Topics.Find(TopicID);
            return new TopicModel(tp);
        }

        public Topic getTopic(int ID)
        {
            var x = db.Topics.Find(ID);
            return(x);
        }

        public List<ValueAttribute> getValueAttribute(string pattern)
        {
            List<ValueAttribute> list = new List<ValueAttribute>();
            List<string> tem1 = new List<string>();
            string p1 = "@@";
            string p2 = "@!";
            if (pattern != "" && pattern != null)
            {
                Regex myRegex = new Regex(p1);
                tem1 = myRegex.Split(pattern).ToList();
                foreach(string x in tem1){
                    Regex tempRegex = new Regex(p2);
                    List<string> temp2 = tempRegex.Split(x).ToList();
                    if (temp2.Count == 2)
                    {
                        list.Add(new ValueAttribute(temp2.ElementAt(0), temp2.ElementAt(1)));
                    }
                    
                }
            }
            return list;
        }
        public int countTopicPostOfUser(int UserID)
        {
            int x = db.Topics.Where(tp => tp.UserID == UserID).Count();
            return x;
        }
        public List<string> getLocationList(string LocationString)
        {

            List<string> list = new List<string>();
            if (LocationString != "" && LocationString != null)
            {
                string p = " {0,}, {0,}";
                Regex myRegex = new Regex(p);
                list = myRegex.Split(LocationString).ToList();
                return list;
            }
            else return list;

        }
        public TopicPage getTopicHasTheItem(int IDtopic,int IdPage,int sizePage) 
        {
            Topic topic = db.Topics.Find(IDtopic);

            string type = "Lost";
            if (topic.Type.Equals("Lost"))
                type = "Get";

            List<Item> items = new List<Item>();

            List<TopicModel> tpModel = new List<TopicModel>();
            List<Topic> topics = new List<Topic>();
            foreach(var i in topic.Items)
            {
                var result = db.Items.Where(m => m.Name.Equals(i.Name));
                foreach (var it in result)
                {
                    items.Add(it);
                }
            }

                foreach (var i in items)
                {
                    if ((!topics.Contains(i.Topic)) && (i.Topic.Type.Equals(type)))
                    {
                        topics.Add(i.Topic);
                    }
                }
            
            

            //foreach(var t in topics)
            //{
            //    tpModel.Add(new TopicModel(t));
            //}
            //return tpModel;
            var filter = (from f in topics select f).Skip((IdPage - 1) * sizePage).Take(sizePage).ToList();
            foreach (var t in filter)
            {
                tpModel.Add(new TopicModel(t));
            }
            TopicPage tpPage = new TopicPage() { Topics = tpModel, PageId = IdPage, PageSize = sizePage, TopicCount = topics.Count };
            return (tpPage);
        }
       

            
    }
    public class ValueAttribute {
        public string Attribute;
        public string Value;
        public ValueAttribute(string att, string val) 
        {
            this.Attribute = att;
            this.Value = val;
        }
    }
    
}